package com.tyun.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.tyun.common.utils.BaseConstant;
import com.tyun.modules.sys.entity.SysMenu;
import com.tyun.modules.sys.mapper.SysMenuMapper;
import com.tyun.modules.sys.service.ISysMenuService;
import com.tyun.modules.sys.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author congcong tao
 * @date 2018/1/24 10:34
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper,SysMenu> implements ISysMenuService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> queryUserMenuList(String userId) {
        List<String> menuIdList = sysUserService.queryMenuIdByUserId(userId);
        List<SysMenu> sysMenuList = this.queryListParentId("0", menuIdList);
        return this.queryMenuTreeList(sysMenuList, menuIdList);
    }

    @Override
    public List<SysMenu> queryListParentId(String parentId, List<String> menuIdList) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setParentId(parentId);
        EntityWrapper<SysMenu> ew = new EntityWrapper<>(sysMenu);
        List<SysMenu> sysMenuList = super.selectList(ew);
        List<SysMenu> userMenuList = new ArrayList<>();
        for(SysMenu menu : sysMenuList){
            if(menuIdList.contains(menu.getMenuId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<Map<String, Object>> roleMenuTreeData(SysMenu sysMenu, String roleId) {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        EntityWrapper<SysMenu> ew = new EntityWrapper<>(sysMenu);
        List<SysMenu> sysMenuList = this.selectList(ew);
        if (StringUtils.isNotEmpty(roleId)) {
            // 获取menuId
            List<String> menuIdList = sysUserService.queryMenuIdByRoleId(roleId);
            trees = getTrees(sysMenuList, true, menuIdList, true);
        } else {
            trees = getTrees(sysMenuList, false, null, true);
        }
        return trees;
    }

    /**
     * 递归查询
     * @param sysMenuList
     * @param menuIdList
     */
    private List<SysMenu> queryMenuTreeList(List<SysMenu> sysMenuList, List<String> menuIdList) {
        List<SysMenu> subMenuList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (!StringUtils.equals(sysMenu.getType(), BaseConstant.MenuType.BUTTON.getValue())) {
                List<SysMenu> sysMenuParentList = this.queryListParentId(sysMenu.getMenuId(), menuIdList);
                sysMenu.setChildren(queryMenuTreeList(sysMenuParentList, menuIdList));
            }
            subMenuList.add(sysMenu);
        }
        return subMenuList;
    }

    public List<Map<String, Object>> getTrees(List<SysMenu> menuList, boolean isCheck, List<String> roleMenuList,
                                              boolean permsFlag) {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        for (SysMenu menu : menuList) {
            Map<String, Object> deptMap = new HashMap<String, Object>();
            deptMap.put("id", menu.getMenuId());
            deptMap.put("pId", menu.getParentId());
            deptMap.put("name", menu.getName());
            if (isCheck) {
                deptMap.put("checked", roleMenuList.contains(menu.getMenuId()));
            } else {
                deptMap.put("checked", false);
            }
            trees.add(deptMap);
        }
        return trees;
    }
}
